package com.blockchain.console.service.content.achieve;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import com.blockchain.console.config.ConsoleConst;
import com.blockchain.console.service.content.FriendlyLinkManage;
import com.blockchain.console.service.content.entity.FriendlyLink;
import com.blockchain.console.service.content.entity.FriendlyLinkRecord;
import com.blockchain.console.service.content.query.FriendlyLinkQuery;
import com.blockchain.framework.data.sql.SQLConnectionProvider;
import com.blockchain.framework.service.ServiceFactory;
import com.blockchain.framework.service.ServiceResource;
import com.blockchain.framework.service.exception.ParameterException;
import com.blockchain.framework.service.query.ArrayParser;
import com.blockchain.framework.service.query.ItemParser;
import com.blockchain.framework.service.query.Paging;
import com.blockchain.framework.service.query.PagingResult;
import com.blockchain.util.StringHelper;

public class FriendlyLinkManageImpl extends AbstractInformationService
		implements FriendlyLinkManage {

	public static class FriendlyLinkManageFactory implements
			ServiceFactory<FriendlyLinkManage> {

		@Override
		public FriendlyLinkManage newInstance(ServiceResource serviceResource) {
			return new FriendlyLinkManageImpl(serviceResource);
		}
	}

	public FriendlyLinkManageImpl(ServiceResource serviceResource) {
		super(serviceResource);
	}

	protected static final ArrayParser<FriendlyLinkRecord> ARRAY_PARSER = new ArrayParser<FriendlyLinkRecord>() {

		@Override
		public FriendlyLinkRecord[] parse(ResultSet resultSet)
				throws SQLException {
			ArrayList<FriendlyLinkRecord> list = null;
			while (resultSet.next()) {
				FriendlyLinkRecord record = new FriendlyLinkRecord();
				record.id = resultSet.getInt(1);
				record.viewTimes = resultSet.getInt(2);
				record.sortIndex = resultSet.getInt(3);
				record.name = resultSet.getString(4);
				record.url = resultSet.getString(5);
				record.publisherId = resultSet.getInt(6);
				record.createTime = resultSet.getTimestamp(7);
				record.updateTime = resultSet.getTimestamp(8);
				record.publisherName = resultSet.getString(9);
				if (list == null) {
					list = new ArrayList<>();
				}
				list.add(record);
			}
			return list == null || list.size() == 0 ? null : list
					.toArray(new FriendlyLinkRecord[0]);
		}
	};
	protected static final ItemParser<FriendlyLinkRecord> ITEM_PARSER = new ItemParser<FriendlyLinkRecord>() {

		@Override
		public FriendlyLinkRecord parse(ResultSet resultSet)
				throws SQLException {
			FriendlyLinkRecord record = null;
			if (resultSet.next()) {
				record = new FriendlyLinkRecord();
				record.id = resultSet.getInt(1);
				record.viewTimes = resultSet.getInt(2);
				record.sortIndex = resultSet.getInt(3);
				record.name = resultSet.getString(4);
				record.url = resultSet.getString(5);
				record.publisherId = resultSet.getInt(6);
				record.createTime = resultSet.getTimestamp(7);
				record.updateTime = resultSet.getTimestamp(8);
				record.publisherName = resultSet.getString(9);
				record.nameEn = resultSet.getString(10);
				record.nameTw = resultSet.getString(11);
			}
			return record;
		}
	};

	protected static final String SELECT_ALL_SQL = "SELECT T5014.F01,T5014.F02,T5014.F03,T5014.F04,T5014.F05,T5014.F06,T5014.F07,T5014.F08 AS PX,T7011.F04 AS F09,T5014.F09 AS nameEn,T5014.F10 AS nameTw FROM T5014 INNER JOIN "
			+ ConsoleConst.DB_CONSOLE + ".T7011 ON T5014.F06 = T7011.F01 ";

	@Override
	public PagingResult<FriendlyLinkRecord> search(FriendlyLinkQuery query,
			Paging paging) throws Throwable {
		ArrayList<Object> parameters = new ArrayList<Object>();
		StringBuilder sql = new StringBuilder(SELECT_ALL_SQL);
		sql.append(" WHERE 1=1 ");
		if (query != null) {
			SQLConnectionProvider connectionProvider = getSQLConnectionProvider();
			String string = query.getName();
			if (!StringHelper.isEmpty(string)) {
				sql.append(" AND T5014.F04 LIKE ?");
				parameters.add(connectionProvider.allMatch(string));
			}
			string = query.getPublisherName();
			if (!StringHelper.isEmpty(string)) {
				sql.append(" AND T7011.F04 LIKE ?");
				parameters.add(connectionProvider.allMatch(string));
			}
			Timestamp timestamp = query.getCreateTimeStart();
			if (timestamp != null) {
				sql.append(" AND DATE(T5014.F07) >= ?");
				parameters.add(timestamp);
			}
			timestamp = query.getCreateTimeEnd();
			if (timestamp != null) {
				sql.append(" AND DATE(T5014.F07) <= ?");
				parameters.add(timestamp);
			}
			timestamp = query.getUpdateTimeStart();
			if (timestamp != null) {
				sql.append(" AND DATE(T5014.F08) >= ?");
				parameters.add(timestamp);
			}
			timestamp = query.getUpdateTimeEnd();
			if (timestamp != null) {
				sql.append(" AND DATE(T5014.F08) <= ?");
				parameters.add(timestamp);
			}
		}
		sql.append(" ORDER BY PX DESC");
		return selectPaging(getConnection(), ARRAY_PARSER, paging,
				sql.toString(), parameters);
	}

	@Override
	public FriendlyLinkRecord get(int id) throws Throwable {
		if (id <= 0) {
			return null;
		}
		return select(getConnection(), ITEM_PARSER, SELECT_ALL_SQL
				+ " WHERE T5014.F01 = ?", id);
	}

	@Override
	public void delete(int... ids) throws Throwable {
		if (ids == null || ids.length == 0) {
			return;
		}
		serviceResource.openTransactions();
		try (Connection connection = getConnection();
				PreparedStatement pstmt = connection
						.prepareStatement("DELETE FROM T5014 WHERE F01 = ?");) {
			for (int id : ids) {
				if (id <= 0) {
					continue;
				}
				pstmt.setInt(1, id);
				pstmt.addBatch();
			}
			pstmt.executeBatch();
		}

	}

	@Override
	public int add(FriendlyLink friendlyLink) throws Throwable {
		if (friendlyLink == null) {
			throw new ParameterException("没有指定友情链接信息 ");
		}
		String name;
		String url;
		{
			name = friendlyLink.getName();
			if (StringHelper.isEmpty(name)) {
				throw new ParameterException("名称不能为空 ");
			}
			url = friendlyLink.getURL();
			if (StringHelper.isEmpty(url)) {
				throw new ParameterException("链接地址不能为空 ");
			}
		}
		return insert(
				getConnection(),
				"INSERT INTO T5014 SET F03 = ?,F04 = ?,F05 = ?,F06 = ?,F07 = CURRENT_TIMESTAMP(),f09=?,f10=?",
				friendlyLink.getSortIndex(), name, url, serviceResource
						.getSession().getAccountId(),friendlyLink.getNameEn(),friendlyLink.getNameTw());
	}

	@Override
	public void update(int id, FriendlyLink friendlyLink) throws Throwable {
		if (id <= 0 || friendlyLink == null) {
			return;
		}
		String name;
		String url;
		{
			name = friendlyLink.getName();
			if (StringHelper.isEmpty(name)) {
				throw new ParameterException("名称不能为空 ");
			}
			url = friendlyLink.getURL();
			if (StringHelper.isEmpty(url)) {
				throw new ParameterException("链接地址不能为空 ");
			}
		}
		execute(getConnection(),
				"UPDATE T5014 SET F03 = ?, F04 = ?,F05 = ?,F09 = ?,F10 = ? WHERE F01 = ?",
				friendlyLink.getSortIndex(), friendlyLink.getName(),
				friendlyLink.getURL(), friendlyLink.getNameEn(), friendlyLink.getNameTw(),id);

	}

}
